package com.jerei.utils.condition;

import com.jerei.utils.UtilsCommon;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunfeihu on 2016/11/3.
 */
public class ConditionTools {
    public static SQLEntity conditionEntities2SQL(ConditionEntity conditionEntity) {
        List<WhereEntity> whereEntities = conditionEntity.getWhereEntities();
        List<OrderByEntity> orderByEntities = conditionEntity.getOrderByEntities();
        SQLEntity sqlEntity = new SQLEntity();
        String sql = "";
        String order = "";
        List<String> params = new ArrayList<String>();
        if (null != whereEntities && whereEntities.size() > 0) {
            String[] vArr = null;
            for (WhereEntity ce : whereEntities) {
                if (ce.getOp().equals(ConditionOp.eq)) {//等于
                    if (ce.getValue().equals("null")) {
                        sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " is null ";
                    } else {
                        sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " = ? ";
                        params.add(ce.getValue());
                    }
                } else if (ce.getOp().equals(ConditionOp.ge)) {//大于等于
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " >= ? ";
                    params.add(ce.getValue());
                } else if (ce.getOp().equals(ConditionOp.gt)) {//大于
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " > ? ";
                    params.add(ce.getValue());
                } else if (ce.getOp().equals(ConditionOp.le)) {//小于等于
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " <= ? ";
                    params.add(ce.getValue());
                } else if (ce.getOp().equals(ConditionOp.lt)) {//小于
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " < ? ";
                    params.add(ce.getValue());
                } else if (ce.getOp().equals(ConditionOp.startsWith)) {//以开头
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " like ? ";
                    params.add("%" + ce.getValue());
                } else if (ce.getOp().equals(ConditionOp.endWith)) {//以结尾
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " like ? ";
                    params.add(ce.getValue() + "%");
                } else if (ce.getOp().equals(ConditionOp.nStartsWith)) {//不以开头
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " not like ? ";
                    params.add("%" + ce.getValue());
                } else if (ce.getOp().equals(ConditionOp.nEndWith)) {//不以结尾
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " not like ? ";
                    params.add(ce.getValue() + "%");
                } else if (ce.getOp().equals(ConditionOp.like)) {//模糊
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " like ? ";
                    params.add("%" + ce.getValue() + "%");
                } else if (ce.getOp().equals(ConditionOp.nlike)) {//不模糊
                    sql += UtilsCommon.camelToUnderline(ce.getField()) + " not like ? ";
                    params.add("%" + ce.getValue() + "%");
                } else if (ce.getOp().equals(ConditionOp.notIn)) {//不包括
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " not in ( ";
                    vArr = ce.getValue().split(",");
                    if (null != vArr && vArr.length > 0) {
                        for (int i = 0; i < vArr.length; i++) {
                            sql += "?,";
                            if (vArr[i].equals("null")) {
                                params.add(null);
                            } else {
                                params.add(vArr[i]);
                            }
                        }
                        sql = sql.substring(0, sql.length() - 1);
                    }
                    sql += ") ";
                } else if (ce.getOp().equals(ConditionOp.notIn)) {//包括
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " in ( ";
                    vArr = ce.getValue().split(",");
                    if (null != vArr && vArr.length > 0) {
                        for (int i = 0; i < vArr.length; i++) {
                            sql += "?,";
                            if (vArr[i].equals("null")) {
                                params.add(null);
                            } else {
                                params.add(vArr[i]);
                            }
                        }
                        sql = sql.substring(0, sql.length() - 1);
                    }
                    sql += ") ";
                } else if (ce.getOp().equals(ConditionOp.between)) {
                    sql += " and " + UtilsCommon.camelToUnderline(ce.getField()) + " between ? and ? ";
                    params.add(ce.getValue());
                    params.add(ce.getValueEnd());
                }
            }
        }
        if (null != orderByEntities && orderByEntities.size() > 0) {
            for (OrderByEntity orderByEntity : orderByEntities) {
                order += UtilsCommon.camelToUnderline(orderByEntity.getOrder()) + " " + orderByEntity.getOp();
            }
        } else {
            order = " id desc ";
        }
        String[] paramArr = new String[params.size()];
        sqlEntity.setParams(params.toArray(paramArr));
        sqlEntity.setSql(sql);
        sqlEntity.setOrder(order);
        return sqlEntity;
    }
}